package com.linln.admin.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linln.common.vo.JsonResult;
import com.linln.common.vo.PageResult;
import com.linln.modules.integralmall.domain.ServiceType;
import com.linln.modules.integralmall.service.ServiceTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Arrays;



/**
 * @author 薛超
 * @since 2021-01-05
 * @version 1.0.7
 */
@RequestMapping("/service/type")
@Controller
public class ServiceTypeController {



	@Autowired
	private ServiceTypeService serviceTypeService;


	@RequiresPermissions("service:type:add")
	@RequestMapping("/add")
	public String add() {
		return "/service/type/add";
	}


	/**
	 * 列表页面
	 */
	@GetMapping("/index")
	@RequiresPermissions("service:type:index")
	public String index() {
		return "/service/type/list";
	}



	@RequestMapping("/list")
	@ResponseBody
	public JsonResult list(Page<ServiceType> page //
	) { // 分页
		LambdaQueryWrapper<ServiceType> queryWrapper = Wrappers.lambdaQuery();
		return JsonResult.ok().put("data",serviceTypeService.page(page, queryWrapper));
	}





	@RequestMapping("/edit/{serviceTypeId}")
	public String edit(@PathVariable("serviceTypeId") String serviceTypeId, Model model) {
		ServiceType serviceType = serviceTypeService.getById(serviceTypeId);
		model.addAttribute("serviceType", serviceType);
		return "/service/type/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	public JsonResult save(ServiceType serviceType) {
		boolean result = serviceTypeService.save(serviceType);
		return result ? JsonResult.ok() : JsonResult.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public JsonResult update(ServiceType serviceType) {
		boolean result = serviceTypeService.updateById(serviceType);
		return result ? JsonResult.ok() : JsonResult.error();
	}

	/**
	 * 删除
	 */

	@RequestMapping("/remove")
	@ResponseBody
	public JsonResult remove(@NotNull(message = "serviceTypeId 不能为空") String serviceTypeId) {
		boolean result = serviceTypeService.removeById(serviceTypeId);
		return result ? JsonResult.ok() : JsonResult.error();
	}

	/**
	 * 批量删除
	 */


	@RequestMapping("/batchRemove")
	@ResponseBody
	public JsonResult batchRemove(
			@NotNull(message = "不能为空") @Size(min = 1, message = "请至少选择一个") @RequestParam("serviceTypeIds[]") String[] serviceTypeIds) {
		boolean result = serviceTypeService.removeByIds(Arrays.asList(serviceTypeIds));
		return result ? JsonResult.ok() : JsonResult.error();
	}


	@GetMapping("integralMallType")
	@ResponseBody
	public PageResult<ServiceType> getIntegralMallType() {
		return new PageResult<ServiceType>(serviceTypeService.getIntegralMallServiceTypeList());
	}

}